<?php
/**
 * 非当前公众号对应的商户账号的公众号支付
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/6
 * Time: 20:15
 */
class WechatThirdPayService
{
    /**
     * 得到微信OpenId(支付到非当前公众号时需要重新获取)
     */
    public function getOpenIdForThirdPay($new_wechat_account_id,$authCode = ''){
        $wechatInfo = M('Wechat')->where(array('account_id' => $new_wechat_account_id))->find();
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $backUrl = urlencode($protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
        if(!$authCode || $authCode == session('authCode')) { //静默授权
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $wechatInfo['appid'] . '&redirect_uri=' . $backUrl . '&response_type=code&scope=snsapi_base&state=recharging#wechat_redirect';
            header('Location: ' . $url);
            exit();
        } else {
            session('authCode', $authCode);
            vendor('Wechat.WechatApi');
            $cWechat = new WechatApi($wechatInfo['wechat_id']);
            $info = $cWechat->getUserInfoByAuth($authCode, false);
            $openid = $info['data'];
            return $openid;
        }
    }
}